﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NLBS.Entity;
using NLBS.Common;

namespace NLBS.Data
{
   public class CommonDataProvider
    {

       public static Category PopulateCategoryFromIDataReader(IDataReader dr)
       {
           Category category = new Category();
           category.CategoryID = Convert.ToInt32(dr["cat_id"]);
           category.Name = Convert.ToString(dr["cat_name"]);
           category.Order = Convert.ToInt32(dr["cat_order"]);
           category.ArticleCount = Convert.ToInt32(dr["cat_articleCount"]);
           category.Hidden = Convert.ToBoolean(dr["cat_hidden"]);
           category.Locked = Convert.ToBoolean(dr["cat_locked"]);

           return category;
       }

       public static Article PopulateNoCatArticleFromIDataReader(IDataReader dr)
       {
           Article article = new Article();
           article.ArticleID = Convert.ToInt32(dr["log_id"]);
           article.CategoryID = Convert.ToInt32(dr["log_catID"]);
           article.Title = Convert.ToString(dr["log_title"]);
           article.AuthorID = Convert.ToInt32(dr["log_authorID"]);
           article.Author = Convert.ToString(dr["log_author"]);
           article.EditMark = Convert.ToString(dr["log_editMark"]);
           article.TrackbackURL = Convert.ToString(dr["log_trackbackURL"]);
           article.Content0 = Convert.ToString(dr["log_content0"]);
           article.Content1 = Convert.ToString(dr["log_content1"]);
           article.Mode = Convert.ToInt16(dr["log_mode"]);
           article.Locked = Convert.ToBoolean(dr["log_locked"]);
           article.Selected = Convert.ToBoolean(dr["log_selected"]);
           article.UbbFlags = Convert.ToString(dr["log_ubbFlags"]);
           article.PostTime = Convert.ToDateTime(dr["log_postTime"]);
           article.IP = Convert.ToString(dr["log_ip"]);
           article.CommentCount = Convert.ToInt32(dr["log_commentCount"]);
           article.TrackBackCount = Convert.ToInt32(dr["log_trackbackCount"]);
           article.ViewCount = Convert.ToInt32(dr["log_viewCount"]);
           article.CategoryName = Convert.ToString(dr["cat_name"]);
           article.IsTop = Convert.ToBoolean(dr["log_istop"]);
           article.EnableInRss = Convert.ToBoolean(dr["log_EnableRss"]);
           return article;
       }
       public static Article PopulateTopAndCommentArticleFromIDataReader(IDataReader dr)
       {
           Article article = PopulateNoCatArticleFromIDataReader(dr);
           article.CategoryName = Convert.ToString(dr["cat_name"]);
           article.IsTop = Convert.ToBoolean(dr["log_istop"]);
           article.EnableInRss = Convert.ToBoolean(dr["log_EnableRss"]);
           //article.EnableComment = Convert.ToBoolean(dr["log_EnableComment"]);
           return article;
       }

       public static Article PopulateArticleFromIDataReader(IDataReader dr)
       {
           Article article = PopulateNoCatArticleFromIDataReader(dr);
           article.CategoryName = Convert.ToString(dr["cat_name"]);
           return article;
       }

       public static Setting PopulateSettingFromIDataReader(IDataReader dr)
       {
           Setting setting = new Setting();
           setting.Name = Convert.ToString(dr["set_name"]);
           setting.Type = Convert.ToInt32(dr["set_type"]);
           setting.Value0 = Convert.ToInt32(dr["set_value0"]);
           setting.Value1 = Convert.ToString(dr["set_value1"]);
           return setting;
       }

       public static User PopulateUserViewAndEditFromIDataReader(IDataReader dr)
       {

           User user=PopulateUserFromIDataReader(dr);
           
           user.GroupName = Convert.ToString(dr["groupName"]);
           return user;
       }

       public static User PopulateUserFromIDataReader(IDataReader dr)
       {
           
           User user = new User();
           user.UserID = Convert.ToInt32(dr["user_id"]);
           user.UserName = Convert.ToString(dr["user_name"]);
           user.Password = Convert.ToString(dr["user_password"]);
           user.Salt = Convert.ToString(dr["user_salt"]);
           user.GroupID = Convert.ToInt32(dr["user_groupID"]);
           user.Gender = Convert.ToInt32(dr["user_gender"]);
           user.Email = Convert.ToString(dr["user_email"]);
           user.HideEmail = Convert.ToBoolean(dr["user_hideEmail"]);
           user.Homepage = Convert.ToString(dr["user_homepage"]);
           user.ArticleCount = Convert.ToInt32(dr["user_articleCount"]);
           user.CommentCount = Convert.ToInt32(dr["user_commentCount"]);
           user.LastVisit = Convert.ToDateTime(dr["user_lastVisit"]);
           user.IP = Convert.ToString(dr["user_ip"]);
           user.HashKey = Convert.ToString(dr["user_hashKey"]);
          
           return user;
       }

       public static UserGroup PopulateUserGroupFromIDataReader(IDataReader dr)
       {
           UserGroup group = new UserGroup();
           group.GroupID = Convert.ToInt32(dr["group_id"]);
           group.Name = Convert.ToString(dr["group_name"]);
           group.Rights = Convert.ToString(dr["group_rights"]);
           return group;
       }

       public static Smile PopulateSmileFromIDataReader(IDataReader dr)
       {
           Smile sm = new Smile();
           sm.SmID = Convert.ToInt32(dr["sm_id"]);
           sm.Image = Convert.ToString(dr["sm_image"]);
           sm.Code = Convert.ToString(dr["sm_code"]);
           return sm;
       }

       public static TrackBack PopulateTrackBackFromIDataReader(IDataReader dr)
       {
           TrackBack tb = new TrackBack();
           tb.ID = Convert.ToInt32(dr["tb_id"]);
           tb.LogID = Convert.ToInt32(dr["log_id"]);
           tb.Url = Convert.ToString(dr["tb_url"]);
           tb.Title = Convert.ToString(dr["tb_title"]);
           tb.BlogName = Convert.ToString(dr["tb_blog"]);
           tb.Excerpt = Convert.ToString(dr["tb_excerpt"]);
           tb.IP = Convert.ToString(dr["tb_ip"]);
           tb.Time = Convert.ToDateTime(dr["tb_time"]);
           return tb;
       }

       public static Comment PopulateCommentFromIDataReader(IDataReader dr)
       {
           Comment comment = new Comment();
           comment.CommentID = Convert.ToInt32(dr["comm_id"]);
           comment.LogID = Convert.ToInt32(dr["log_id"]);
           comment.Content = Convert.ToString(dr["comm_content"]);
           comment.AuthorID = Convert.ToInt32(dr["comm_authorID"]);
           comment.Author = Convert.ToString(dr["comm_author"]);
           comment.EditMark = Convert.ToString(dr["comm_editMark"]);
           comment.Hidden = Convert.ToBoolean(dr["comm_hidden"]);
           comment.UbbFlags = Convert.ToString(dr["comm_ubbFlags"]);
           comment.PostTime = Convert.ToDateTime(dr["comm_postTime"]);
           comment.IP = Convert.ToString(dr["comm_ip"]);
           return comment;
       }

       public static GuestBook PopulateGuestBookFromIDataReader(IDataReader dr)
       {
           GuestBook guestBook = new GuestBook();
           guestBook.GuestBookID = Convert.ToInt32(dr["gb_id"]);
           guestBook.UserName = Convert.ToString(dr["gb_userName"]);
           guestBook.UserID = Convert.ToInt32(dr["gb_userID"]);
           guestBook.Content = Convert.ToString(dr["gb_content"]);
           guestBook.EditMark = Convert.ToString(dr["gb_editMark"]);
           guestBook.UbbFlags = Convert.ToString(dr["gb_ubbFlags"]);
           guestBook.PostTime = Convert.ToDateTime(dr["gb_postTime"]);
           guestBook.ReplyUserName = Convert.ToString(dr["gb_replyUserName"]);
           guestBook.Reply = Convert.ToString(dr["gb_reply"]);
           
           guestBook.ReplyTime =TypeParse.SafeDateTime(dr["gb_replyTime"].ToString());
           guestBook.Hidden = Convert.ToBoolean(dr["gb_hidden"]);
           guestBook.IP = Convert.ToString(dr["gb_ip"]);
           return guestBook;
       }

       public static Archive PopulateArchiveFromIDataReader(IDataReader dr)
       {
           Archive archive = new Archive();
           archive.Count = Convert.ToInt32(dr["Count"]);
           
           archive.Date = new DateTime(Convert.ToInt32(dr["Year"]), Convert.ToInt32(dr["Month"]), Convert.ToInt32(dr["Day"]));
           return archive;
       }

       public static ArticleLink PopulateArticleLinkFromIDataReader(IDataReader dr)
       {
           ArticleLink link = new ArticleLink();
           link.Id = Convert.ToInt32(dr["ID"]);
           link.Title = Convert.ToString(dr["title"]);
           link.PostTime = Convert.ToDateTime(dr["postTime"]);
           return link;
       }

       public static int PopulateTotalRecordsFromIDataReader(IDataReader dataReader)
       {
           dataReader.NextResult();
           dataReader.Read();
           return Convert.ToInt32(dataReader["TotalRecords"]);
       }

       public static WordFilter PopulateWrodFilterFromIDataReader(IDataReader dr)
       {
           WordFilter wf = new WordFilter();
           wf.WordFilterID = Convert.ToInt32(dr["wf_id"]);
           wf.Mode = Convert.ToByte(dr["wf_mode"]);
           wf.Text = Convert.ToString(dr["wf_text"]);
           wf.Replace = Convert.ToString(dr["wf_replace"]);
           wf.RegExp = Convert.ToBoolean(dr["wf_regExp"]);
           return wf;
       }

       public static VisitorRecord PopulateVisitorRecordFromIDataReader(IDataReader dr)
       {
           VisitorRecord visitor = new VisitorRecord();
           visitor.ID = Convert.ToInt32(dr["vr_id"]);
           visitor.IP = Convert.ToString(dr["vr_ip"]);
           visitor.OS = Convert.ToString(dr["vr_os"]);
           visitor.Browser = Convert.ToString(dr["vr_Browser"]);
           visitor.Time = Convert.ToDateTime(dr["vr_time"]);
           visitor.Referer = Convert.ToString(dr["vr_referer"]);
           visitor.Target = Convert.ToString(dr["vr_target"]);
           return visitor;
       }

       public static FriendLink PopulateFriendLinkFromIDataReader(IDataReader reader)
       {

           FriendLink item = new FriendLink();
           item.LinkID = reader.GetInt32(reader.GetOrdinal("linkID"));
           item.LinkName = reader.GetString(reader.GetOrdinal("linkName"));
           item.LinkURL = reader.GetString(reader.GetOrdinal("linkURL"));
           item.LinkLogo = reader.GetString(reader.GetOrdinal("linkLogo"));
           item.LinkOrder = reader.GetInt32(reader.GetOrdinal("linkOrder"));
           item.IsMain = reader.GetBoolean(reader.GetOrdinal("linkMain"));
           item.IsArrow = reader.GetBoolean(reader.GetOrdinal("linkArrow"));

           return item;
       }

       public static Widget PopulateWidgetFromIDataReader(IDataReader reader)
       {

           Widget item = new Widget();
           item.WidgetID = reader.GetInt32(reader.GetOrdinal("widgetID"));
           item.Name = reader.GetString(reader.GetOrdinal("widget_name"));
           item.Title = reader.GetString(reader.GetOrdinal("widget_title"));
           item.Content = reader.GetString(reader.GetOrdinal("widget_content"));
           item.Hidden = reader.GetBoolean(reader.GetOrdinal("widget_hidden"));
           item.InstallTime = reader.GetDateTime(reader.GetOrdinal("widget_installTime"));
           item.IsInstall = reader.GetBoolean(reader.GetOrdinal("widget_isinstall"));
           item.Order = reader.GetInt32(reader.GetOrdinal("widget_order"));
           item.Rate = reader.GetInt32(reader.GetOrdinal("widget_rate"));
           item.Type = reader.GetInt32(reader.GetOrdinal("widget_type"));
           //item.Rate = reader.GetBoolean(reader.GetOrdinal("linkArrow"));
           return item;
       }

       public static ExtensionLink PopulateExtensionLinkFromIDataReader(IDataReader dr)
       {
           ExtensionLink item = new ExtensionLink();
           item.LinkID = Convert.ToInt32(dr["linkID"]);
           item.LinkName = Convert.ToString(dr["name"]);
           item.LinkURL = Convert.ToString(dr["url"]);
           item.Extensions = Convert.ToString(dr["extensions"]);
           item.Type = Convert.ToInt32(dr["type"]);
           return item;
       }
    }
}
